﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WingOpenSourceComponentWP;
using System.ComponentModel;

namespace Sample
{
    public class Fruit : INotifyPropertyChanged
    {
        private string _name;
        public string Name 
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }
    public class TestOb : INotifyPropertyChanged
    {
        public TestOb()
        {
            _pined = new BoundProperty<bool>();
            _f = new BoundProperty<Fruit>();
        }
        private BoundProperty<bool> _pined;
        public BoundProperty<bool> Pined
        {
            get
            {
                return _pined;
            }
            set
            {
                _pined = value;
                NotifyPropertyChanged("Pined");
            }
        }

        private BoundProperty<Fruit> _f;
        public BoundProperty<Fruit> F
        {
            get
            {
                return _f;
            }
            set
            {
                _f = value;
                NotifyPropertyChanged("F");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }

    }
    public partial class MainPage : PhoneApplicationPage
    {
        TestOb t = new TestOb();
        public MainPage()
        {
            InitializeComponent();
            t.F = new BoundProperty<Fruit>()
            {
                BoundInstance = new Fruit()
                {
                    Name = "Apple",
                }
            };

            btnTest.DataContext = t.F.BoundInstance;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Test();
        }
        public void Test()
        {
            BoundProperty<bool>.SetBinding(t.Pined, TogglePin);
            BoundProperty<Fruit>.SetBinding(t.F, Compare);

            t.Pined.BoundInstance = true;
            t.F.SetValue(new Fruit() { Name="Orange" });

        }

        void TogglePin(bool Pined)
        {

        }
        void Compare(bool o, bool n)
        {
            string f = "Old value is {0};/n New value is {1}";
            MessageBox.Show(string.Format(f, o, n));
        }
        void Compare(Fruit o, Fruit n)
        {
            string f = "Old value is {0};/n New value is {1}";
            MessageBox.Show(string.Format(f, o.Name, n.Name));
        }

    }
}